package com.wzqj.agent.app.store.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wzqj.agent.app.login.controller.AuthRequiredController;
import com.wzqj.agent.app.merchant.service.ApiMerchantService;
import com.wzqj.agent.app.store.service.ApiStoreService;
import com.wzqj.agent.common.JsonOutput;
import com.wzqj.agent.common.exception.BusinessErrorType;
import com.wzqj.agent.util.CaptchaUtil;
import com.wzqj.agent.util.StoreUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by wanghao@weipass.cn on 2015/8/4.
 */
@Controller
@RequestMapping("/api/store")
public class ApiStoreController extends AuthRequiredController {

    @Autowired
    private ApiStoreService apiStoreService;

    @Autowired
    private ApiMerchantService apiMerchantService;

    /**
     * 绑定店铺到商户的审核状态:1审核中，2审核成功，3审核失败
     */
    private static final Integer CLAIM_STATUS = 2;
    /**
     * 获取商户已有门店列表
     * @return
     */
    @RequestMapping("/getStores")
    @ResponseBody
    public JSONObject getStoreList(String qualification){

        JsonOutput output = new JsonOutput();
        if(StringUtils.isEmpty(qualification)){
            output.setErrCode(BusinessErrorType.PARAM_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.PARAM_ERROR.getErrMsg());
            return output.toJSONObject();
        }
        JSONArray data = apiStoreService.getStoresByQualification(qualification);
        if(data!= null){
        	System.out.println("getStores:"+data);
            output.setData(data);
        }else{
            output.setErrCode(BusinessErrorType.MERCHANT_STORE_SEARCH_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.MERCHANT_STORE_SEARCH_ERROR.getErrMsg());
        }
        return output.toJSONObject();
    }

    /**
     * 获取店铺详情
     * @return
     */
    @RequestMapping("/getStoreInfo")
    @ResponseBody
    public JSONObject getStoreInfo(String storeid){

        JsonOutput output = new JsonOutput();
        if(StringUtils.isEmpty(storeid)){
            output.setErrCode(BusinessErrorType.PARAM_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.PARAM_ERROR.getErrMsg());
            return output.toJSONObject();
        }
        JSONObject data = apiStoreService.getStoreInfoById(storeid);
        if(data!= null){
            output.setData(data);
        }else{
            output.setErrCode(BusinessErrorType.MERCHANT_STORE_SEARCH_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.MERCHANT_STORE_SEARCH_ERROR.getErrMsg());
        }
        return output.toJSONObject();
    }

    /**
     * 获取店铺用户名密码
     * @param mcode
     * @return
     */
    @RequestMapping("/getStoreAuth")
    @ResponseBody
    public JSONObject getStoreAuthInfo(String mcode){

        JsonOutput output = new JsonOutput();
        if(StringUtils.isEmpty(mcode)){
            output.setErrCode(BusinessErrorType.PARAM_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.PARAM_ERROR.getErrMsg());
            return output.toJSONObject();
        }
        String password = CaptchaUtil.generatPassword(6);
        JSONObject data = apiStoreService.getStoreAuthInfo(mcode,password);
        if(data!= null){
            output.setData(data);
        }else{
            output.setErrCode(BusinessErrorType.MERCHANT_STORE_SEARCH_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.MERCHANT_STORE_SEARCH_ERROR.getErrMsg());
        }
        return output.toJSONObject();
    }

    /**
     * 搜索店铺数据来源为百度和长沙
     * @param shopName
     * @param city
     * @param pageIndex
     * @param limit
     * @param lastId
     * @return
     */
    @RequestMapping("/searchStores")
    @ResponseBody
    public JSONObject searchStores(String shopName,String city,int pageIndex,int limit,
                                   @RequestParam(value = "lastId", required = false, defaultValue = "")String lastId ){
        JsonOutput output = new JsonOutput();
        if(StringUtils.isEmpty(shopName) || StringUtils.isEmpty(city)){
            output.setErrCode(BusinessErrorType.PARAM_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.PARAM_ERROR.getErrMsg());
            return output.toJSONObject();
        }
        JSONArray data = apiStoreService.searchStores(shopName, city, pageIndex, limit, lastId);
        if(data!= null){
            output.setData(data);
        }else{
            output.setErrCode(BusinessErrorType.MERCHANT_STORE_SEARCH_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.MERCHANT_STORE_SEARCH_ERROR.getErrMsg());
        }
        return output.toJSONObject();
    }

    /**
     * 添加店铺并绑定到商户下
     * @param qualification 商户资质id
     * @param merId 商户id
     * @return
     */
    @RequestMapping("/addStore")
    @ResponseBody
    public JSONObject addStore(HttpServletRequest req,String qualification,String merId,String nickName){

        JsonOutput output = new JsonOutput();
        JSONObject store = StoreUtil.generalStore(req,merId,nickName);
        JSONObject data = apiStoreService.addStore(store);
        if (data != null){
            String storeId = data.getString("storeId");
            String mcode = data.getString("mcode");
            data = apiMerchantService.bindStore(mcode, storeId, qualification,CLAIM_STATUS);
            if(data != null){
                output.setData(data);
            }else{
                output.setErrCode(BusinessErrorType.MERCHANT_STORE_BIND_ERROR.getErrCode());
                output.setMsg(BusinessErrorType.MERCHANT_STORE_BIND_ERROR.getErrMsg());
            }
        }else{
            output.setErrCode(BusinessErrorType.MERCHANT_STORE_ADD_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.MERCHANT_STORE_ADD_ERROR.getErrMsg());
        }
        return output.toJSONObject();
    }

    /**
     * 更新店铺信息
     * @return
     */
    @RequestMapping("/updateStore")
    @ResponseBody
    public JSONObject updateStore(HttpServletRequest req,String storeId){

        JsonOutput output = new JsonOutput();
        JSONObject store = StoreUtil.generalUpdateStore(req);
        JSONObject data = apiStoreService.updateStore(store,storeId);
        if (data != null){
             output.setData(data);
        }else{
            output.setErrCode(BusinessErrorType.MERCHANT_STORE_UPDATE_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.MERCHANT_STORE_UPDATE_ERROR.getErrMsg());
        }
        return output.toJSONObject();
    }

    /**
     * 绑定门店到商户下
     * @return
     */
    @RequestMapping("/bindStore")
    @ResponseBody
    public JSONObject bindStore(String mcode,String storeid, String qualification,Integer claimStatus){

        JsonOutput output = new JsonOutput();
        if(StringUtils.isEmpty(mcode) || StringUtils.isEmpty(storeid) || StringUtils.isEmpty(qualification)){
            output.setErrCode(BusinessErrorType.PARAM_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.PARAM_ERROR.getErrMsg());
            return output.toJSONObject();
        }
        JSONObject data = apiMerchantService.bindStore(mcode, storeid, qualification,claimStatus);
        if(data != null){
                output.setData(data);
            }else{
                output.setErrCode(BusinessErrorType.MERCHANT_STORE_ADD_ERROR.getErrCode());
                output.setMsg(BusinessErrorType.MERCHANT_STORE_ADD_ERROR.getErrMsg());
            }
        return output.toJSONObject();
    }

    /**
     * 获取行业列表
     * @return
     */
    @RequestMapping("/getBizClassify")
    @ResponseBody
    public JSONObject getBizClassify(){

        JsonOutput output = new JsonOutput();
        JSONArray data = apiStoreService.getBizClassify();
        if(data != null){
            output.setData(data);
        }else{
            output.setErrCode(BusinessErrorType.MERCHANT_STORE_BIZCLASSIFY_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.MERCHANT_STORE_BIZCLASSIFY_ERROR.getErrMsg());
        }
        return output.toJSONObject();
    }

    /**
     * 获取行业列表（新）
     * @return
     */
    @RequestMapping("/getBizClassifyList")
    @ResponseBody
    public JSONObject getBizClassifyList(){
    	JsonOutput output = new JsonOutput();
        JSONArray data = apiStoreService.getBizClassifyList();
        if(data != null){
            output.setData(data);
        }else{
            output.setErrCode(BusinessErrorType.MERCHANT_STORE_BIZCLASSIFY_ERROR.getErrCode());
            output.setMsg(BusinessErrorType.MERCHANT_STORE_BIZCLASSIFY_ERROR.getErrMsg());
        }
        return output.toJSONObject();
    }
}
